package kr.hyunist.androidcomplete01saeildamil.ch12;

import kr.hyunist.androidcomplete01saeildamil.*;
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.*;

public class GridViewEx1 extends Activity {
	int[] picture = { R.drawable.ccdam, R.drawable.soyang2, R.drawable.ududong,
			R.drawable.zipdarigol, R.drawable.dongul };
	GridAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gridviewex1);

		adapter = new GridAdapter(this, picture);
		GridView gridview = (GridView) findViewById(R.id.gridViewEx01);
		gridview.setAdapter(adapter);
		gridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Toast.makeText(GridViewEx1.this, position + "번째 그림 선택!", Toast.LENGTH_SHORT).show();
			}
			
		});
	}
}

class GridAdapter extends BaseAdapter {
	int[] picture;
	Context mContext;

	public GridAdapter(Context context, int[] pic) {
		mContext = context;
		picture = pic;
	}

	@Override
	public int getCount() {
		return 100;
	}

	@Override
	public Object getItem(int position) {
		return picture[position % picture.length];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageview;
		if(convertView == null){
			imageview = new ImageView(mContext);
			imageview.setLayoutParams(new GridView.LayoutParams(144, 144));
			imageview.setAdjustViewBounds(false);
			imageview.setScaleType(ImageView.ScaleType.FIT_XY);
		} else { 
			imageview = (ImageView) convertView;
		}
		imageview.setImageResource(picture[position % picture.length]);
		
		return imageview;
	}

}